package com.network.service;

import com.network.config.DynamicJob;
import com.network.dao.JobEntityRepository;
import com.network.entity.JobEntity;
import com.network.entity.Plan;
import com.network.qo.PageQo;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.PersistenceUnit;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by EalenXie on 2018/6/4 14:25
 */
@Service
public class DynamicJobService {
    @Autowired
    private JobEntityRepository repository;
    //通过Id获取Job
    public JobEntity getJobEntityById(Long id) {
        return repository.getById(id);
    }
    //从数据库中加载获取到所有Job
    public List<JobEntity> loadJobs() {
        List<JobEntity> list = new ArrayList<>();
        repository.findAll().forEach(list::add);
        return list;
    }

    public Page<JobEntity> findPlanByPage(PageQo pageQo)
    {
        //根据创建时间排序
        Sort sort=new Sort(Sort.Direction.DESC,"id");
        Pageable pageable=new PageRequest(pageQo.getPage(),pageQo.getLimit(),sort);
        return repository.findAll((root, query, criteriaBuilder) -> {
            List<Predicate> predicateList=new ArrayList<Predicate>();
            query.where(predicateList.toArray(new Predicate[predicateList.size()]));
            return query.getRestriction();
        },pageable);
    }

    public void delJobByPid(JobEntity jobEntity)
    {
         repository.delete(jobEntity);
    }

    //获取JobDataMap.(Job参数对象)
    public JobDataMap getJobDataMap(JobEntity job) {
        JobDataMap map = new JobDataMap();
        map.put("id", job.getId());
        map.put("name", job.getName());
        map.put("group", job.getGroup());
        map.put("cronExpression", job.getCron());
        map.put("parameter", job.getParameter());
        map.put("JobDescription", job.getDescription());
        map.put("vmParam", job.getVmParam());
        map.put("jarPath", job.getJarPath());
        map.put("status", job.getStatus());
        return map;
    }
    //获取JobDetail,JobDetail是任务的定义,而Job是任务的执行逻辑,JobDetail里会引用一个Job Class来定义
    public JobDetail geJobDetail(JobKey jobKey, String description, JobDataMap map) {
        return JobBuilder.newJob(DynamicJob.class)
                .withIdentity(jobKey)
                .withDescription(description)
                .setJobData(map)
                .storeDurably()
                .build();
    }
    //获取Trigger (Job的触发器,执行规则)
    public Trigger getTrigger(JobEntity job) {
        return TriggerBuilder.newTrigger()
                .withIdentity(job.getName(), job.getGroup())
                .withSchedule(CronScheduleBuilder.cronSchedule(job.getCron()))
                .build();
    }
    //获取JobKey,包含Name和Group
    public JobKey getJobKey(JobEntity job) {
        return JobKey.jobKey(job.getName(), job.getGroup());
    }
}